package org.shop.controller.app;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.Attention;
import org.shop.entity.Favorites;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.FavoritesService;
import org.shop.util.ObjUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Administrator
 *
 */
@Controller
public class FavoritesController {

	private static Logger logger = LogManager.getLogger(FavoritesController.class);
	
	@Autowired
	private FavoritesService favoritesService;
	

	/** 
	* @Title: focusAndCancelFocus 
	* @Description: TODO(取消收藏或者是收藏) 
	* @param @param req
	* @param @return     
	* @return int    返回类型 
	* @throws 
	*/
	@RequestMapping("app/focusAndCancelFocus")
	@ResponseBody
	public Map<String, Object> focusAndCancelFocus(HttpServletRequest req){
		Map<String, Object> map = new HashMap<String, Object>();
		//获取登录人用户信息
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Long targetId = Long.parseLong(req.getParameter("targetId"));
		String favoriteType =req.getParameter("favoriteType");
		
		try {
			Favorites favorites = new Favorites();
			favorites.setUserId(ud.getUserId());
			favorites.setTargetId(targetId);
			Favorites favorites2 = favoritesService.getFavoritesByTargetIdAndFavoriteType(favorites);
			if(ObjUtil.isNotNullAndEmpty(favorites2)) {
				favoritesService.deleteByPrimaryKey(favorites2.getFavoritesId());
				map.put("state", 2);
				map.put("msg", "取消收藏成功");
			}else {
				Favorites fa = new Favorites();
				fa.setFavoriteType(Byte.parseByte(favoriteType));
				fa.setUserId(ud.getUserId());
				fa.setTargetId(targetId);
				favoritesService.insertSelective(fa);
				map.put("state", 1);
				map.put("msg", "收藏成功");
			}
			
		}catch(Exception e) {
			map.put("state", 0);
			map.put("msg", "操作失败");
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 获取我收藏的商品
	 * @param req
	 * @return
	 */
	@RequestMapping("app/getFavoritesList")
	public String getFavoritesList(HttpServletRequest req) {
		//获取登录人用户信息
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", ud.getUserId());
			//获取一手商品
			params.put("favoriteType", 0);
			List<Map<String, Object>> favoritesList = favoritesService.getFavoritesList(params);
			req.setAttribute("favoritesList", favoritesList);
			//获取二手商品
			params.put("favoriteType", 1);
			List<Map<String, Object>> favoritesUsedList = favoritesService.getFavoritesList(params);
			req.setAttribute("favoritesUsedList", favoritesUsedList);
		}catch(Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return "app/favorites";
	}
	
	/**
	 * 查询我是否收藏了此商品
	 * @param req
	 * @return
	 */
	@RequestMapping("app/getFavorites")
	@ResponseBody
	public Map<String, Object> getAttention(HttpServletRequest req) {
		Map<String, Object> map = new HashMap<String, Object>();
		//获取登录人用户信息
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String targetId = req.getParameter("targetId"); 
		String attentionType = req.getParameter("attentionType");
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", ud.getUserId());
			params.put("targetId", targetId);
			params.put("attentionType", attentionType);
			map.put("favoritesGoodsList", favoritesService.getFavoritesList(params));
			map.put("state", 1);
			map.put("msg", "获取成功");
		}catch(Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			map.put("state", 0);
			map.put("msg", "获取失败");
		}
		return map;
	}

	
}
